<template>
  <div class="style-page">
    <div class="line"></div>
    <div class="head-title">{{ showComponentData.title }}</div>
    <div class="line"></div>

    <!-- 驾校名称 -->
    <div class="component-style">
      <div class="header" @click="collapsedName = !collapsedName">
        <p class="title">驾校名称样式</p>
        <el-icon v-if="!collapsedName"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
      <el-form v-if="!collapsedName" :model="form.nameStyle" label-width="auto">
        <el-form-item label="内容">
          <el-input v-model="showComponentData.name" />
        </el-form-item>
        <el-form-item label="字号">
          <el-slider v-model="form.nameStyle.fontSize" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.nameStyle.color" />
        </el-form-item>
        <el-form-item label="字体粗细">
          <el-input type="number" v-model="form.nameStyle.fontWeight" />
        </el-form-item>
      </el-form>
    </div>

    <!-- 标签配置 -->
    <div class="component-style">
      <div class="header" @click="collapsedTags = !collapsedTags">
        <p class="title">标签配置</p>
        <el-icon v-if="!collapsedTags"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
      <div v-if="!collapsedTags">
        <p class="tip">标签内容（可拖拽排序）</p>
        <VueDraggable
          v-model="showComponentData.tags"
          :animation="150"
          ghostClass="ghost"
          class="content"
        >
          <div class="tag-item" v-for="(tag, index) in showComponentData.tags" :key="index">
            <el-input v-model="showComponentData.tags[index]" placeholder="标签文字" />
            <el-button type="danger" plain icon="Delete" @click="removeTag(index)" />
          </div>
        </VueDraggable>
        <el-button plain icon="Plus" type="primary" @click="addTag" style="width: 100%">
          添加标签
        </el-button>

        <el-form :model="form.tagStyle" label-width="auto" class="mt10">
          <el-form-item label="字号">
            <el-slider v-model="form.tagStyle.fontSize" />
          </el-form-item>
          <el-form-item label="字体颜色">
            <el-color-picker v-model="form.tagStyle.color" />
          </el-form-item>
          <el-form-item label="背景颜色">
            <el-color-picker v-model="form.tagStyle.backgroundColor" />
          </el-form-item>
          <el-form-item label="圆角">
            <el-slider v-model="form.tagStyle.borderRadius" />
          </el-form-item>
          <el-form-item label="字体粗细">
            <el-input type="number" v-model="form.tagStyle.fontWeight" />
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 营业时间 -->
    <div class="component-style">
      <div class="header" @click="collapsedTime = !collapsedTime">
        <p class="title">营业时间样式</p>
        <el-icon v-if="!collapsedTime"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
      <el-form v-if="!collapsedTime" :model="form.timeStyle" label-width="auto">
        <el-form-item label="内容">
          <el-input v-model="showComponentData.businessHours" />
        </el-form-item>
        <el-form-item label="字号">
          <el-slider v-model="form.timeStyle.fontSize" :min="10" :max="24" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.timeStyle.color" />
        </el-form-item>
        <el-form-item label="字体粗细">
          <el-input type="number" v-model="form.timeStyle.fontWeight" />
        </el-form-item>
      </el-form>
    </div>

    <!-- 地址主文案 -->
    <div class="component-style">
      <div class="header" @click="collapsedAddrMain = !collapsedAddrMain">
        <p class="title">地址主文案样式</p>
        <el-icon v-if="!collapsedAddrMain"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
      <el-form v-if="!collapsedAddrMain" :model="form.addressMainStyle" label-width="auto">
        <el-form-item label="内容">
          <el-input v-model="showComponentData.address" />
        </el-form-item>
        <el-form-item label="字号">
          <el-slider v-model="form.addressMainStyle.fontSize" :min="10" :max="24" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.addressMainStyle.color" />
        </el-form-item>
        <el-form-item label="字体粗细">
          <el-input type="number" v-model="form.addressMainStyle.fontWeight" />
        </el-form-item>
      </el-form>
    </div>

    <!-- 地址副文案 -->
    <div class="component-style">
      <div class="header" @click="collapsedAddrSub = !collapsedAddrSub">
        <p class="title">地址副文案样式</p>
        <el-icon v-if="!collapsedAddrSub"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
      <el-form v-if="!collapsedAddrSub" :model="form.addressSubStyle" label-width="auto">
        <el-form-item label="字号">
          <el-slider v-model="form.addressSubStyle.fontSize" :min="10" :max="24" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-color-picker v-model="form.addressSubStyle.color" />
        </el-form-item>
        <el-form-item label="字体粗细">
          <el-input type="number" v-model="form.addressSubStyle.fontWeight" />
        </el-form-item>
      </el-form>
    </div>

    <div class="component-style">
      <common-style
        :content-data="showComponentData"
        @update-style="handleUpdateStyle"
      ></common-style>
    </div>

  </div>
</template>

<script setup lang="ts">
  import { reactive, ref, toRaw, watch } from 'vue'
  import { ArrowDown, ArrowUp } from '@element-plus/icons-vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import debounce from 'lodash/debounce'
  import CommonStyle from '@/views/decoration/diyComponents/CommonStyleComponent/CommonStyle.vue'

  const props = defineProps({
    contentData: {
      type: Object,
      default: () => ({})
    }
  })
  const emit = defineEmits(['updateData'])
  const showComponentData = ref(props.contentData)

  const collapsed = ref(false)
  const collapsedName = ref(false)
  const collapsedTags = ref(false)
  const collapsedTime = ref(false)
  const collapsedAddrMain = ref(false)
  const collapsedAddrSub = ref(false)

  const form = reactive({
    nameStyle: { fontSize: 16, color: '#333333', fontWeight: 600 },
    tagStyle: {
      fontSize: 12,
      color: '#1890ff',
      backgroundColor: '#e6f7ff',
      borderRadius: 12,
      fontWeight: 600
    },
    timeStyle: { fontSize: 12, color: '#666666', fontWeight: 600 },
    addressMainStyle: { fontSize: 14, color: '#333333', fontWeight: 500 },
    addressSubStyle: { fontSize: 12, color: '#999999', fontWeight: 400 }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)

    showComponentData.value.nameStyle = raw.nameStyle
    showComponentData.value.timeStyle = raw.timeStyle
    showComponentData.value.tagStyle = raw.tagStyle
    showComponentData.value.addressMainStyle = raw.addressMainStyle
    showComponentData.value.addressSubStyle = raw.addressSubStyle

    handleUpdateComponent()
  }, 300)

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }

  function addTag() {
    showComponentData.value.tags.push('')
  }

  function removeTag(index: number) {
    showComponentData.value.tags.splice(index, 1)
  }

  function handleUpdateStyle(val: any) {
    showComponentData.value.style = val
    handleUpdateComponent()
  }
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-title {
      padding: 20px 0;
      font-size: 16px;
      font-weight: bold;
    }

    .line {
      width: 100%;
      height: 1px;
      background: #f5f7fa;
    }

    .component-style {
      margin: 20px 0;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;

        .title {
          font-size: 16px;
          font-weight: bold;
        }
      }

      .tag-item {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 10px;
      }

      .tip {
        font-size: 13px;
        color: #999;
        margin-bottom: 8px;
      }

      .mt10 {
        margin-top: 10px;
      }
    }
  }
</style>
